package com.gxa.modules.service.impl;

import com.gxa.common.exception.ManageException;
import com.gxa.common.utils.MD5Utils;
import com.gxa.common.utils.RandomUtils;
import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.R;
import com.gxa.common.vo.TableName;
import com.gxa.modules.mapper.ManageMapper;
import com.gxa.modules.pojo.R_Role;
import com.gxa.modules.pojo.R_User;
import com.gxa.modules.service.ManageService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.annotations.Cacheable;


import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: wangyifan
 * @Date: 2021/12/27 19:04
 * @Description: $end$
 * @Version: V1.0
 */

@Service
public class ManageServiceImpl implements ManageService {

    @Autowired(required = false)
    private ManageMapper manageMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R addRole(String roleName, List<Integer> powers) {
        for (int x:powers) {
            if(x<0 || x> 30){
                return R.error(400,"权限数据有误");
            }
        }

        int result = manageMapper.addRole(roleName);
        //失败回滚
        if(result<=0){ throw new ManageException("添加角色失败");}
        for (int power:powers) {
             result = manageMapper.addRole2(roleName,power);
        }
        if(result<=0){ throw new ManageException("添加角色失败");}
        return new R();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addUser(R_User user) {
        R r = new R();
        String salt = RandomUtils.generateLowerString(6);
        user.setSalt(salt);
        SimpleHash simpleHash = new SimpleHash(MD5Utils.MD5_KEY,user.getUser_password(),salt,MD5Utils.HSAH);
        user.setUser_password(simpleHash.toString());
        int result =manageMapper.addUser(user);
        if(result <= 0 ){throw new ManageException("创建账户失败");}
        int result2 = manageMapper.addUserRole(user.getUsername(),user.getRole().getRole_id());
        if(result2 <= 0 ){throw new ManageException("创建账户失败");}
        int result3 = manageMapper.addAc_num(user.getRole().getRole_id());
        if(result3 <= 0 ){throw new ManageException("创建账户失败");}
        return r;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult queryRole(Integer currentPage, Integer pageSize,String string) {
        Integer count = manageMapper.count(TableName.ROLE);
        Integer currentPage0 = (currentPage-1)*pageSize;
        List<R_Role> roleList = manageMapper.queryRole(currentPage0,pageSize,string);
        JsonResult jsonResult = new JsonResult("查询成功",count,roleList);
        return jsonResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult queryUser(Integer currentPage, Integer pageSize, String string) {
        Integer count = manageMapper.count(TableName.USER);
        Integer currentPage0 = (currentPage-1)*pageSize;
        List<R_User> userList = manageMapper.queryUser(currentPage0,pageSize,string);
        JsonResult jsonResult = new JsonResult("查询成功",count,userList);
        return jsonResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateRole(Integer roleId,@NotNull String roleName, List<Integer> powers) {
        for (int x:powers) {
            if(x<0 || x> 30){
                return R.error(400,"权限数据有误");
            }
        }
        int result = manageMapper.updateRole(roleId,roleName);
        if(result <= 0 ){throw new ManageException("编辑角色失败");}
         result = manageMapper.deleteRoleMenu(roleId);
        if(result <= 0 ){throw new ManageException("编辑角色失败");}
        for (int power:powers) {
            result = manageMapper.addRoleMnue(roleId,power);
        }
        if(result <= 0 ){throw new ManageException("编辑角色失败");}
        return new R();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteRole(@NotNull Integer roleId) {
        int result = manageMapper.deleteRoleMenu(roleId);
        if(result <= 0 ){throw new ManageException("删除角色失败");}
        result = manageMapper.deleteRole(roleId);
        if(result <= 0 ){throw new ManageException("删除角色失败");}
        return new R();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateUser(R_User user) {
        String salt = RandomUtils.generateLowerString(6);
        user.setSalt(salt);
        SimpleHash simpleHash = new SimpleHash("md5",user.getUser_password(),"salt",2);
        user.setUser_password(simpleHash.toString());
        int result =manageMapper.updateUser(user);
        if(result <= 0 ){throw new ManageException("编辑账户失败");}
        result = manageMapper.updateUserRole(user.getUser_id(),user.getRole().getRole_id());
        if(result <= 0 ){throw new ManageException("编辑账户失败");}
        return new R();
    }

    @Override
    public R deleteUser(@NotNull Long userId) {
        int result = manageMapper.deleteUser(userId);
        if(result <= 0 ){throw new ManageException("删除账户失败");}
        result =manageMapper.deleteUserRole(userId);
        if(result <= 0 ){throw new ManageException("删除账户失败");}
        return new R();
    }


}
